<template>
  <div>
    <a-form
      ref="formRef"
      :model="formState"
      :rules="rules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
      <a-form-item
        label="姓名"
        name="name"
        style="width: 700px; border-radius: 15px"
      >
        <a-input v-model:value="formState.name" />
      </a-form-item>
      <a-form-item
        label="电话"
        :name="['sub', 'name']"
        style="width: 700px; border-radius: 15px"
      >
        <a-input v-model:value="formState.sub.name" />
      </a-form-item>

      <a-form-item
        :wrapper-col="{ span: 14, offset: 4 }"
        style="margin-left: 35px"
      >
        <a-button type="primary" @click="onSubmit">注册</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script>
import { defineComponent, reactive, ref, toRaw } from "vue";
export default defineComponent({
  setup() {
    const formRef = ref();
    const formState = reactive({
      name: undefined,
      sub: {
        name: undefined,
      },
    });
    const rules = {
      name: [
        {
          required: true,
          message: "请输入姓名，要求必须是中文并且名字长度2-4位",
          pattern:/^[\u4e00-\u9fa5]{2,4}$/,
          min: 2,
          max: 4,
        },
      ],
      sub: {
        name: [
          {
            required: true,
            message: "请输入电话",
          },
          {
            min: 11,
            max: 11,
            message: "电话格式 不正确",
            pattern: /^((0\d{2,3}-\d{7,8})|(1[34578]\d{9}))$/,
            trigger: "blur",
          },
        ],
      },
    };

    const onSubmit = () => {
      formRef.value
        .validate()
        .then(() => {
          console.log("values", formState, toRaw(formState));
        })
        .catch((error) => {
          console.log("error", error);
        });
    };
    return {
      formRef,
      labelCol: {
        span: 4,
      },
      wrapperCol: {
        span: 14,
      },
      formState,
      rules,
      onSubmit,
    };
  },
});
</script>

<style>
</style>